home *** CD-ROM | disk | FTP | other *** search
/ Mission 3 / Mission 3.zip / Mission 3.iso / texte / qed / src / error.c < prev    next >
C/C++ Source or Header  |  1998-11-09  |  7KB  |  384 lines

  1. #include <ctype.h>
  2. #include <support.h>
  3.  
  4. #include "global.h"
  5. #include "edit.h"
  6. #include "icon.h"
  7. #include "memory.h"
  8. #include "rsc.h"
  9. #include "set.h"
  10. #include "text.h"
  11. #include "window.h"
  12. #include "error.h"
  13.  
  14. extern void    menu_help(int title, int item);
  15.  
  16. /*
  17.  * Exportierte Variablen:
  18. */
  19. char    error[FEHLERANZ][40];
  20. TEXTP    last_errtext = NULL;
  21.  
  22. /*
  23.  * lokales
  24. */
  25. typedef enum {nil, read_text, read_name, read_zeile, read_spalte, read_fehler} TOKEN;
  26.  
  27. #define MAX_TOKEN        10                /* Anzahl der Token */
  28. #define MAX_ERRLEN    120            /* Länge der Fehlerzeile */
  29.  
  30. typedef struct
  31. {
  32.     TOKEN    token;
  33.     char    text[30];
  34. } TOKENELEM, *TEP;
  35.  
  36. static TOKENELEM    token_list[MAX_TOKEN];
  37. static int            token_anzahl;
  38. static PATH            error_name;                    /* Dateiname des Errorfiles */
  39.  
  40. /* das Ergebnis */
  41. static PATH    dateiname;
  42. static char    fehlertext[MAX_ERRLEN];
  43. static long    fehlerzeile;
  44. static int    fehlerspalte;
  45. static int    err_anz = 0;
  46.  
  47.  
  48.  
  49. static void init_parser(char *muster)
  50. {
  51.     char    tmp[2] = " ";
  52.     int    i;
  53.     TOKEN last_token;
  54.  
  55.     strcpy(dateiname, "");
  56.     fehlerzeile = -1;
  57.     fehlerspalte = -1;
  58.     strcpy(fehlertext, "");
  59.  
  60.     token_anzahl = 0;
  61.     last_token = nil;
  62.     for (i = 0; i < MAX_TOKEN; i++)
  63.     {
  64.         token_list[i].token = nil;
  65.         strcpy(token_list[i].text, "");
  66.     }
  67.  
  68.     for (i = 0; i < (int)strlen(muster); i++)
  69.     {
  70.         switch (muster[i])
  71.         {
  72.             case '%' :
  73.                 i++;
  74.                 if (last_token == read_text)
  75.                     token_anzahl++;
  76.                 switch (muster[i])
  77.                 {
  78.                     case 'f' :
  79.                         token_list[token_anzahl].token = read_name;
  80.                         last_token = read_name;
  81.                         break;
  82.                     case 'z' :
  83.                         token_list[token_anzahl].token = read_zeile;
  84.                         last_token = read_zeile;
  85.                         break;
  86.                     case 's' :
  87.                         token_list[token_anzahl].token = read_spalte;
  88.                         last_token = read_spalte;
  89.                         break;
  90.                     case 't' :
  91.                         token_list[token_anzahl].token = read_fehler;
  92.                         last_token = read_fehler;
  93.                         break;
  94.                 }
  95.                 break;
  96.             default:
  97.                 if (last_token > read_text)
  98.                     token_anzahl++;
  99.                 token_list[token_anzahl].token = read_text;
  100.                 tmp[0] = muster[i];
  101.                 strcat(token_list[token_anzahl].text, tmp);
  102.                 last_token = read_text;
  103.                 break;
  104.         }
  105.     }
  106. }
  107.  
  108.  
  109. static bool    readin_text(char *zeile, int *pos, char *text)
  110. {
  111.     int    len = (int)strlen(text),
  112.             i;
  113.     char    tmp[10];
  114.  
  115.     i = *pos;
  116.     while ( (i < (int)strlen(zeile)) && (i < (len + *pos)))
  117.     {
  118.         tmp[i - *pos] = zeile[i];
  119.         i++;
  120.     }
  121.     if (i > *pos)
  122.     {
  123.         tmp[i - *pos] = EOS;
  124.         *pos = i;
  125.         return (strcmp(tmp, text) == 0);
  126.     }
  127.     else
  128.         return FALSE;
  129. }
  130.  
  131. static bool    readin_name(char *zeile, int *pos)
  132. {
  133.     SET    valid_char;
  134.     PATH    tmp;
  135.     int    i;
  136.  
  137.     strcpy(tmp,"-+._~\\/A-Za-z0-9");                /* Zulässige Zeichen für Dateinamen */
  138.     str2set(tmp, valid_char);
  139.     i = *pos;
  140.     while (     (i < (int)strlen(zeile)) &&     /* Sonderbehandlung für ':', nur im Pfad erlaubt! */
  141.                 ((setin(valid_char, zeile[i])) ||
  142.                  (zeile[i] == ':' && (zeile[i+1] == '\\') || zeile[i+1] == '/')))
  143.     {
  144.         tmp[i - *pos] = zeile[i];
  145.         i++;
  146.     }
  147.     if (i > *pos)
  148.     {
  149.         tmp[i - *pos] = EOS;
  150.         *pos = i;
  151.  
  152.         if (strchr(tmp, '/') != NULL)                /* UNIX-Pfad -> nach TOS wandeln */
  153.         {
  154.             unx2dos(tmp, dateiname);
  155.         }
  156.         else if (tmp[1] != ':')                        /* Kein Laufwerk -> Name ohne Pfad! */
  157.         {
  158.  
  159.             split_filename(error_name, dateiname, NULL);
  160.             strcat(dateiname, tmp);
  161.         }
  162.         else
  163.             strcpy(dateiname, tmp);
  164.         return (file_exists(dateiname));
  165.     }
  166.     else
  167.         return FALSE;
  168. }
  169.  
  170. static bool    readin_zeile(char *zeile, int *pos)
  171. {
  172.     int    i;
  173.     char    tmp[10];
  174.  
  175.     i= *pos;
  176.     while ( (i < (int)strlen(zeile)) && (isdigit(zeile[i])) )
  177.     {
  178.         tmp[i - *pos] = zeile[i];
  179.         i++;
  180.     }
  181.     if (i > *pos)
  182.     {
  183.         tmp[i - *pos] = EOS;
  184.         fehlerzeile = atol(tmp);
  185.         *pos = i;
  186.         return TRUE;
  187.     }
  188.     else
  189.         return FALSE;
  190. }
  191.  
  192. static bool    readin_spalte(char *zeile, int *pos)
  193. {
  194.     int    i;
  195.     char    tmp[10];
  196.  
  197.     i= *pos;
  198.     while ( (i < (int)strlen(zeile)) && (isdigit(zeile[i])) )
  199.     {
  200.         tmp[i - *pos] = zeile[i];
  201.         i++;
  202.     }
  203.     if (i > *pos)
  204.     {
  205.         tmp[i - *pos] = EOS;
  206.         fehlerspalte = atoi(tmp);
  207.         *pos = i;
  208.         return TRUE;
  209.     }
  210.     else
  211.         return FALSE;
  212. }
  213.  
  214. static bool    readin_fehler(char *zeile, int *pos)
  215. {
  216.     int    i, j;
  217.     char    tmp[MAX_ERRLEN];
  218.  
  219.     i = *pos;
  220.     j = 0;
  221.     while ( (i < (int)strlen(zeile)) && (j < sizeof(tmp)) )
  222.     {
  223.         tmp[i - *pos] = zeile[i];
  224.         i++;
  225.         j++;
  226.     }
  227.     if (i > *pos)
  228.     {
  229.         tmp[i - *pos] = EOS;
  230.         *pos = i;
  231.         strcpy(fehlertext, tmp);
  232.         return TRUE;
  233.     }
  234.     else
  235.         return FALSE;
  236. }
  237.  
  238. static bool    parse_line(char *zeile)
  239. {
  240.     int    i, z_pos = 0;
  241.     bool    ok = FALSE;
  242.  
  243.     for (i = 0; i <= token_anzahl; i++)
  244.     {
  245.         switch (token_list[i].token)
  246.         {
  247.             case read_text :
  248.                 ok = readin_text(zeile, &z_pos, token_list[i].text);
  249.                 break;
  250.             case read_name :
  251.                 ok = readin_name(zeile, &z_pos);
  252.                 break;
  253.             case read_zeile :
  254.                 ok = readin_zeile(zeile, &z_pos);
  255.                 break;
  256.             case read_spalte :
  257.                 ok = readin_spalte(zeile, &z_pos);
  258.                 break;
  259.             case read_fehler :
  260.                 ok = readin_fehler(zeile, &z_pos);
  261.                 break;
  262.         }
  263.         if (!ok)
  264.             break;
  265.     }
  266.     return ok;;
  267. }
  268.  
  269.  
  270. void    handle_error(TEXTP t_ptr)
  271. {
  272.     int    icon, i;
  273.     char    str[256];
  274.  
  275.     if (last_errtext != NULL && t_ptr != last_errtext)
  276.     {
  277.         ZEILEP lauf = t_ptr->cursor_line;
  278.     
  279.         t_ptr = last_errtext;
  280.         lauf = t_ptr->cursor_line;
  281.         /* nächste Zeile setzen */        
  282.         if (!IS_LAST(t_ptr->cursor_line))
  283.         {
  284.             NEXT(lauf);
  285.             t_ptr->cursor_line = lauf;
  286.             t_ptr->xpos = 0;
  287.             t_ptr->ypos++;
  288.             make_chg(t_ptr->link, POS_CHANGE, 0);
  289.         }
  290.     }
  291.     for (i = 0; i < err_anz; i++)
  292.     {
  293.         init_parser(error[i]);
  294.         strcpy(error_name, t_ptr->filename);
  295.         if (parse_line(TEXT(t_ptr->cursor_line)))
  296.         {
  297.             last_errtext = t_ptr;
  298.  
  299.             icon = load_edit(dateiname, FALSE);            /* Laden als Text und öffnen */
  300.             if (icon > 0)
  301.             {
  302.                 if (fehlerspalte > 0)
  303.                     desire_x = fehlerspalte - 1;                /* wir fange bei 0 an! */
  304.                 else
  305.                     desire_x = 0;
  306.                 if (fehlerzeile > 0)
  307.                     desire_y = fehlerzeile - 1;
  308.                 else
  309.                     desire_y = 0;
  310.  
  311.                 if (strlen(fehlertext) > 0)
  312.                 {
  313.                     strcpy(str, rsc_string(ERRORSTR));
  314.                     strcat(str, fehlertext);
  315.                     set_info(get_text(icon), str);
  316.                 }
  317.                 icon_edit(icon, DO_GOTO);
  318.                 return;
  319.             }
  320.         }
  321.     }
  322.     if (last_errtext != NULL)
  323.         last_errtext = NULL;
  324.     Bconout(2, 7);
  325. }
  326.  
  327. /*
  328.  * Die Dialogbox für die Fehlerzeile
  329.  */
  330. void    fehler_box(void)
  331. {
  332.     int    antw, i;
  333.     char    str[40];
  334.     MDIAL    *dial;
  335.     bool    close = FALSE;
  336.     
  337.     for (i = 0; i < FEHLERANZ; i++)
  338.         set_string(fehler, FEHLTEXT1 + i, error[i]);
  339.     
  340.     dial = open_mdial(fehler, 0);
  341.     if (dial != NULL)
  342.     {
  343.         while (!close)
  344.         {
  345.             antw = do_mdial(dial) & 0x7fff;
  346.             switch (antw)
  347.             {
  348.                 case FEHLHELP :
  349.                     menu_help(TSPEZIAL, MFEHLER);
  350.                     set_state(fehler, antw, SELECTED, FALSE);
  351.                     redraw_mdobj(dial, antw);
  352.                     break;
  353.                 default:
  354.                     close = TRUE;
  355.                     break;
  356.             }
  357.         }
  358.         close_mdial(dial);
  359.         set_state(fehler, antw, SELECTED, FALSE);
  360.         if (antw == FEHLOK)
  361.         {
  362.             err_anz = 0;
  363.             for (i = 0; i < FEHLERANZ; i++)
  364.             {
  365.                 strcpy(error[i], "");                                /* leeren */
  366.                 get_string(fehler, FEHLTEXT1 + i, str);
  367.                 set_errorline(str);
  368.             }
  369.         }
  370.     }
  371. }
  372.  
  373. /*
  374.  * Zeilen aus Parameterdatei eintragen.
  375. */
  376. void set_errorline(char *zeile)
  377. {
  378.     if ((err_anz < FEHLERANZ) && (zeile[0] != EOS))
  379.     {
  380.         strcpy(error[err_anz], zeile);
  381.         err_anz++;
  382.     }
  383. }
  384.